'use strict';

const { Controller } = require('egg');

class HomeController extends Controller {

  async setreport() {
    let isLogin = this.ctx.service.user.isLogin()
    if (!isLogin) {
      this.ctx.body = { code: 4003, info: "请登录" }
      return
    }
    let uid = this.ctx.session.uid
    let { reporttext, gid } = this.ctx.request.query
    let sql1 = `INSERT INTO report (uid,gid,TEXT) VALUE (${uid},${gid},"${reporttext}")`
    await this.ctx.app.mysql.query(sql1)
    this.ctx.body = { code: 2006, info: "评论成功" }
  }
  async getreport() {
    let { gid, start = 0 } = this.ctx.request.query
    // let sql1 = `SELECT * FROM report WHERE gid=${gid} limit ${start},5 `
    let sql1 = `SELECT rid,TEXT,img,nickname from user inner join report on report.uid=user.uid WHERE report.gid=${gid} order by rid DESC limit ${start},5`
    let arr = await this.ctx.app.mysql.query(sql1)
    this.ctx.body = { code: 2200, info: arr }
  }
  async detail() {
    let { gid } = this.ctx.request.query
    let sql1 = `SELECT *  FROM   goods where gid=${gid}`
    let arr = await this.ctx.app.mysql.query(sql1)
    this.ctx.body = { code: 2100, info: arr[0] }

  }
  async goodstotal() {
    let { keywords = "" } = this.ctx.request.query
    let sql1 = `SELECT COUNT(gid)  FROM   goods where title like "%${keywords}%"`
    let arr = await this.ctx.app.mysql.query(sql1)
    console.log(arr[0]["COUNT(gid)"])
    this.ctx.body = { code: 2008, info: arr[0]["COUNT(gid)"] }
  }
  async getgoods() {

    let { keywords, start = 0 } = this.ctx.request.query
    let sql1 = `select * from goods where title like "%${keywords}%" limit ${start},5`
    console.log(1111111111111, sql1)
    let arr = await this.ctx.app.mysql.query(sql1)
    this.ctx.body = { code: 2008, info: arr }
  }
  async sub() {
    let { title, price, count, img } = this.ctx.request.query
    let sql1 = `insert into goods (title, price, count, img) values ("${title}","${price}","${count}","${img}")`
    this.ctx.app.mysql.query(sql1)
    this.ctx.body = { code: 2008, info: "添加成功" }
  }
}

module.exports = HomeController;
